﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;

namespace coffeeshop.Forms
{
    public partial class frmProductList : Form
    {
        private bool formLoaded = false;

        public frmProductList()
        {
            InitializeComponent();
            LoadProducts(true);
            setButtonEditEnable();
        }

        private void setButtonEditEnable()
        {
            btnEdit.Enabled = listProduct.SelectedItems.Count > 0;
        }

        public void ReloadList()
        {
            LoadProducts(ckboxHideInactive.Checked);
            setButtonEditEnable();
        }

        private void LoadProducts(bool toHideInactive)
        {
            listProduct.Clear();
            listProduct.ItemCheck += new ItemCheckEventHandler(listProduct_ItemCheck);
            listProduct.View = View.Details;
            // Display check boxes.
            listProduct.CheckBoxes = true;
            // Select the item and subitems when selection is made.
            listProduct.FullRowSelect = true;
            // Display grid lines.
            listProduct.GridLines = true;

            //coffeeDataSet.ProductDataTable _list = Product.GetProductList(toHideInactive);
            coffeeDataSet.ProductDataTable _list = Product.GetProductListWithIngredients(toHideInactive);
            listProduct.Columns.Add("+", -1, HorizontalAlignment.Left);
            listProduct.Columns.Add("Tên", 100, HorizontalAlignment.Left);
            listProduct.Columns.Add("Mã", 40, HorizontalAlignment.Left);
            listProduct.Columns.Add("Nhóm", 40, HorizontalAlignment.Left);
            listProduct.Columns.Add("ĐL", 40, HorizontalAlignment.Left);
            listProduct.Columns.Add("Giá", 70, HorizontalAlignment.Left);
            listProduct.Columns.Add("Công thức", 270, HorizontalAlignment.Left);
            listProduct.Columns.Add("ProductId", 0, HorizontalAlignment.Left);

            foreach (DataRow row in _list.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Checked = (bool)row["IsActive"];
                item.SubItems.Add(row["Name"].ToString());
                item.SubItems.Add(row["Code"].ToString());
                item.SubItems.Add(row["CategoryName"].ToString());
                item.SubItems.Add(row["UnitName"].ToString());
                item.SubItems.Add(row["Price"].ToString());
                item.SubItems.Add(row["TheIngredients"].ToString());
                item.SubItems.Add(row["ProductId"].ToString());

                listProduct.Items.Add(item);
            }
        }

        private void listProduct_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
        {
            if (!formLoaded)
                return;

            bool isActive = e.CurrentValue == CheckState.Unchecked;

            Product theProduct = new Product(int.Parse(listProduct.Items[e.Index].SubItems[7].Text));
            theProduct.IsActive = isActive;

            if (!theProduct.UpdateActiveness())
            {
                MessageBox.Show("Có lỗi khi thay đổi kích hoạt của mặt hàng");
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frmEditProduct editForm = new frmEditProduct(null);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void frmProductList_Shown(object sender, EventArgs e)
        {
            formLoaded = true;
        }

        private void ckboxHideInactive_CheckedChanged(object sender, EventArgs e)
        {
            if (!formLoaded)
                return;

            bool isChecked = ((CheckBox)sender).Checked;

            LoadProducts(isChecked);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listProduct.SelectedItems.Count == 0)
            {
                MessageBox.Show("Hãy chọn một mặt hàng trước");
                return;
            }
            ListViewItem selectedItem = listProduct.SelectedItems[0];
            Product theProduct = new Product(int.Parse(selectedItem.SubItems[7].Text));

            frmEditProduct editForm = new frmEditProduct(theProduct);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void listProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            setButtonEditEnable();
        }
    }
}
